package com.shizhanzhe.zxbapp.vip;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.NoticeDeatil;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.utils.Util;

import me.wcy.htmltext.HtmlImageLoader;
import me.wcy.htmltext.HtmlText;

/**
 * Created by zz9527 on 2018/11/2.
 */
public class NoticeDetailActivity extends Activity {
    private  TextView content;
    private WebView webView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noticedetail);
        ImmersionBar.with(this).statusBarColor(R.color.them)
                .fitsSystemWindows(true).init();
        findViewById(R.id.backarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView title = findViewById(R.id.title);
        webView = findViewById(R.id.web);
        webView.setBackgroundColor(0);
        webView.getSettings().setJavaScriptEnabled(false);
        webView.getSettings().setTextSize(WebSettings.TextSize.LARGEST);
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.getSettings().setUseWideViewPort(true);//设置webview显示屏幕宽度 不能滑动
        webView.getSettings().setLoadWithOverviewMode(true);
        content = findViewById(R.id.content);
        content.setMovementMethod(ScrollingMovementMethod.getInstance());// 设置可滚动
        int type = getIntent().getIntExtra("type", 0);
            getData(type);
    }

    private void getData(int type) {
        String path="";
        if (type==1){
            path=Path.XSDETAIL(getIntent().getIntExtra("id", 0));
        }else if (type==2){
            path=Path.GZDETAIL(getIntent().getIntExtra("id", 0));
        }else if (type==3){
            path=Path.NOTICEDETAIL(getIntent().getIntExtra("id", 0));
        }

        OkHttpDownloadJsonUtil.downloadJson(this, path, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    Gson gson = new Gson();
                    NoticeDeatil bean = gson.fromJson(json, NoticeDeatil.class);
                    if (!"".equals(bean.getData().getDetails())) {

                        json = bean.getData().getDetails().replace("src=\"", "src=\"http://zhuanxiaobao.com");
                        webView.loadDataWithBaseURL(null, json, "text/html", "utf-8", null);

//                content.setText(Html.fromHtml(json,new MyImageGetter(getApplicationContext(),content),null));
                        HtmlText.from(json)
                                .setImageLoader(new HtmlImageLoader() {
                                    @Override
                                    public void loadImage(String url, final Callback callback) {
                                        // Glide sample, you can also use other image loader
                                        Glide.with(getApplicationContext()).asBitmap()
                                                .load(url)
                                                .into(new SimpleTarget<Bitmap>() {
                                                    @Override
                                                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                                        callback.onLoadComplete(resource);
                                                    }
                                                });
                                    }

                                    @Override
                                    public Drawable getDefaultDrawable() {
                                        return ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_launcher);
                                    }

                                    @Override
                                    public Drawable getErrorDrawable() {
                                        return ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_launcher);
                                    }

                                    @Override
                                    public int getMaxWidth() {
                                        return Util.getScreenWidth(getApplicationContext());
                                    }

                                    @Override
                                    public boolean fitWidth() {
                                        return false;
                                    }
                                }).into(content);
                    } else {
                        content.setText(bean.getData().getContent());
                    }
                }else {
                    Toast.makeText(NoticeDetailActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy(); //必须调用该方法，防止内存泄漏
    }
}
